<?php
/**
 * Created by PhpStorm.
 * User: Raymond
 * Date: 2020-03-23
 * Time: 13:14
 */
$json = file_get_contents('class.json');

$class = json_decode($json,true);

if (json_last_error()){
    var_dump(json_last_error_msg());exit;
}
$chapters = $class['terms'][0]['chapter_info'][0]['sub_info'];
//print_r(array_keys($chapters));

//print_r($chapters);

function one_line($item,$time = ''){
    $item = str_replace([",","，"],[' ',' '],$item);
    if (empty($time)){
        $item = $item . "," . 0 . "\r\n";
    }else{
        $time = str_replace("分钟",'',$time);
        $item = $item . "," . $time . "\r\n";
    }
    return $item;
}

$str = '';

foreach ($chapters as $chapter) {
    $str .= one_line($chapter['name']);
    foreach ($chapter['task_info'] as $item) {
        $str .= one_line($item['name'],$item['suffixText']);
    }
}
echo $str;

file_put_contents('class.csv',$str);